package command
{
   import com.greensock.TweenMax;
   import com.greensock.events.TweenEvent;
   
   import jp.progression.commands.Command;
   
   /**
    * @author cjboy | cjboy1984@gmail.com
    * @date   Aug,30,2010
    * @usage
    * addCommand(
    *    new DoTweenMax(mc, 1, {x:100, y:100, alpha:1})
    * );
    */
   public class DoTweenMax extends Command
   {
      private var tween:TweenMax;
      private var target:Object;
      private var parameters:Object;
      private var duration:Number;
      
      public function DoTweenMax(tar:Object, dur:Number, param:Object, initObj:Object = null)
      {
         target = tar;
         duration = dur <= 0 ? 0.01 : dur;
         parameters = param;
         
         super(myExecute, myInterrupt, initObj);
      }
      
      // ################### protected ##################
      
      override public function executeComplete():void
      {
         super.executeComplete();
         tween = null;
      }
      
      override public function clone():Command
      {
         return new DoTweenMax(target, duration, parameters);
      }
      
      // #################### private ###################
      
      private function myExecute():void
      {
         tween = new TweenMax(target, duration, parameters);
         tween.addEventListener(TweenEvent.COMPLETE, myComplete, false, int.MAX_VALUE, true);
      }
      
      private function myComplete(e:TweenEvent):void
      {
         tween.removeEventListener(TweenEvent.COMPLETE, myComplete, false);
         // override executeComplete();
         executeComplete();
      }
      
      private function myInterrupt():void
      {
         tween.kill();
         super.interrupt();
      }
      
      // --------------------- LINE ---------------------
      
   }
   
}